<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20231015
 * Time: 11:05 
 * 功能模块
 */
namespace app\admin\model;

use app\admin\model\Model;
use Mscore\Core\Lib\ArrayMethod;

class Rule extends Model{

	private $module = 'sys_rules';
    
    private $error=['code'=>0,'msg'=>'fail'];
    private $success=['code'=>200,'msg'=>'success'];

    /**
     * 功能列表
     *  * @param array $list
     * @param array $where
     */
    public function list($where='',$file='*')
    {
        $datalist = Model::name($this->module)
            ->where($where)
            ->field($file)
            ->order('id asc')
            ->select()
            ->toArray();
        return $datalist;       
    }

    /**
     * 模块信息
     * @param array $info
     * @param array $id
     */
    public function info($id=''){
        return Model::name($this->module)->where([
            'id'=>$id
        ])->find();
    }

    /**
     * 模块保存
     * @param array $save
     */
    public function save($data=[])
    {
        $data['operate']['key']=!empty($data['operate']['key'])?$data['operate']['key']:[];
        foreach (array_filter($data['operate']['key']) as $key => $value) {
            $operates[$key]['key']=$value;
            $operates[$key]['name']=$data['operate']['name'][$key];
            $operates[$key]['url']=$data['operate']['url'][$key];
            $operates[$key]['value']=$data['operate']['value'][$key];
        }
        unset($data['operate']);
        $operates=!empty($operates)?json_encode($operates,JSON_UNESCAPED_UNICODE):'';
        $saveData = [
            'title'=>$data['title'],
            'icon'=>$data['icon'],
            'code'=>$data['code'],
            'langkey'=>$data['langkey'],
            'pid'=>$data['pid'],
            'is_top'=>$data['is_top'],
            'status'=>$data['status'],
            'url'=>$data['url'],
            'orderid'=>$data['orderid'],
            'operate'=>$operates
        ];
        switch (true) {
            case !empty($data['id']):
                Model::name($this->module)->where(' id=' . $data['id'])->update($saveData);
                $list=Model::name($this->module)->select()->toArray();
                $list=ArrayMethod::GetTeamMember($list,'pid','id',$data['id'], true);
                // 修改子菜单
                foreach ($list as $key => $value) {
                    Model::name($this->module)->where('id=' . $value['id'])->update(['status'=>$data['status']]); 
                }
                return $this->success;
                break;           
            default:
                Model::name($this->module)->insert($saveData);
                return $this->success;
                break;
        }
        return $this->error;
    }

    /**
     * 功能删除
     * @param array $del
     * @param array $where
     */
    public function del($id='')
    {

        $list=Model::name($this->module)->select()->toArray();
        $list=ArrayMethod::GetTeamMember($list,'pid','id',$id, true);
        switch (true) {
            case !empty($id):
                Model::name($this->module)->where('id=' . $id)->update(['dels'=>1, 'status'=>0]);
                foreach ($list as $key => $value) {
                    Model::name($this->module)->where('id=' . $value['id'])->update(['dels'=>1, 'status'=>0]);
                }
                return $this->success;
                break;
            
            default:
                return $this->success;
                break;
        }
        return $this->error; 
    }
}